import store from 'store2'

// store key
const AUDIT_PREVIEW_FILES_STORE = 'auditPreviewFiles'
// 缓存文件预览数量
const CACHE_PREVIEW_FILES_NUM = 100

export const types = {
  addPreviewFile: 'addPreviewFile',
}

const states = {
  files: store.get(AUDIT_PREVIEW_FILES_STORE, []),
}

const mutations = {
  // 添加预览文件
  [types.addPreviewFile](state, url) {
    if (state.files.indexOf(url) < 0) {
      if (state.files.length >= CACHE_PREVIEW_FILES_NUM) {
        state.files.shift()
      }
      state.files.push(url)
      store.set(AUDIT_PREVIEW_FILES_STORE, state.files)
    }
  },
}

const actions = {
  addPreviewFile({ commit }, url) {
    commit(types.addPreviewFile, url)
  },
}

const getters = {}

export default {
  namespaced: true,
  state: states,
  mutations,
  actions,
  getters,
}
